package com.zzy.furns.service.impl;

import com.zzy.furns.dao.FurnDAO;
import com.zzy.furns.dao.OrderDAO;
import com.zzy.furns.dao.OrderItemDAO;
import com.zzy.furns.dao.impl.FurnDAOImpl;
import com.zzy.furns.dao.impl.OrderDAOImpl;
import com.zzy.furns.dao.impl.OrderItemDAOImpl;
import com.zzy.furns.domain.Cart;
import com.zzy.furns.domain.Furn;
import com.zzy.furns.domain.Order;
import com.zzy.furns.domain.OrderItem;
import com.zzy.furns.service.OrderService;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class OrderServiceImpl implements OrderService {

    private OrderDAO orderDAO = new OrderDAOImpl();
    private OrderItemDAO orderItemDAO = new OrderItemDAOImpl();
    private FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public String saveOrder(Cart cart, Integer memberId) {

        //判断当前用户购物车是否为null或空
        if (cart.getItems().size() == 0){
            System.out.println("购物车已被清空，请重新购买商品后在结账");
            return null;
        }

        //封装Order对象
        Order order = new Order();
        //利用UUID生成随机订单号
        String orderId = UUID.randomUUID().toString();
        order.setId(orderId);
        order.setPrice(cart.getTotalPrice());
        order.setStatus(0);
        order.setMemberId(memberId);
        order.setCreateTime(LocalDateTime.now());

        List<OrderItem> orderItems = new ArrayList<>();
        //遍历购物车，封装OrderItem对象
        cart.getItems().forEach((furnId,cartItem) -> {
            /**
             * 为什么不在循环中直接执行保存到数据库操作？
             *      个人认为：确保数据完全准备充分后在执行保存到数据库操作，
             *      防止中途出现异常，导致有些OrderItem保存，有些OrderItem没有保存
             */
            OrderItem orderItem = new OrderItem();
            orderItem.setName(cartItem.getName());
            orderItem.setPrice(cartItem.getPrice());
            orderItem.setCount(cartItem.getCount());
            orderItem.setTotalPrice(cartItem.getTotalPrice());
            orderItem.setOrderId(orderId);
            orderItems.add(orderItem);
        });

        //保存数据到数据库
        orderDAO.saveOrder(order);
        orderItems.forEach(orderItem -> {
            orderItemDAO.saveOrderItem(orderItem);
        });

        //更新家居表中的销量
        cart.getItems().forEach((furnId,cartItem) -> {
            Furn furn = furnDAO.queryFurnById(furnId);
            furn.setSales(furn.getSales() + cartItem.getCount());
            furnDAO.updateFurn(furn);
        });

        //清空购物车
        cart.getItems().clear();

        return orderId;
    }

    @Override
    public Order queryOrderByOrderId(String orderId) {
        Order order = orderDAO.queryOrderByOrderId(orderId);
        return order;
    }

    @Override
    public List<OrderItem> queryOrderItemListByOrderId(String orderId) {
        return orderItemDAO.queryOrderItemListByOrderId(orderId);
    }

    @Override
    public Integer getTotalCount(List<OrderItem> orderItemList) {
        Integer totalCount = 0;
        for (OrderItem orderItem : orderItemList) {
            totalCount += orderItem.getCount();
        }
        return totalCount;
    }

    @Override
    public BigDecimal getTotalPrice(List<OrderItem> orderItemList) {
        BigDecimal totalPrice = new BigDecimal(0);
        for (OrderItem orderItem : orderItemList) {
            totalPrice = totalPrice.add(orderItem.getTotalPrice());
        }
        return totalPrice;
    }
}
